package com.lingxu.api.ztb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.api.ztb.mapper.ProjectPostEvaluationMapper;
import com.lingxu.api.ztb.model.ProjectPostEvaluation;

import com.lingxu.api.ztb.model.vo.HandoverAuditVO;
import com.lingxu.api.ztb.service.IProjectPostEvaluationService;
import com.lingxu.api.ztb.model.vo.ProjectPostEvaluationVO;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import java.util.*;

/**
 *  项目后评价表  服务实现类
 *
 * @author admin
 * @since 2025-08-15
 */
@Slf4j
@Service
public class ProjectPostEvaluationServiceImpl extends ServiceImpl<ProjectPostEvaluationMapper, ProjectPostEvaluation>  implements IProjectPostEvaluationService {

    @Resource
    private ProjectPostEvaluationMapper projectPostEvaluationMapper;

    @Transactional(readOnly = true)
    @Override
    public IPage<ProjectPostEvaluationVO> queryPageList(ProjectPostEvaluationVO query) {
        Page<ProjectPostEvaluationVO> page = new Page<ProjectPostEvaluationVO>(query.getPageNo(), query.getPageSize());
        return projectPostEvaluationMapper.pageList(page,query);
    }


    @Transactional(readOnly = true)
    @Override
    public List<ProjectPostEvaluationVO> list(ProjectPostEvaluationVO query) {
        return projectPostEvaluationMapper.selectProjectPostEvaluationList(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Set<String> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            projectPostEvaluationMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public ProjectPostEvaluationVO pageList(ProjectPostEvaluationVO query) {
        List<ProjectPostEvaluationVO> list = projectPostEvaluationMapper.queryPageInfo(query.getProjectName());
        if (list == null || list.isEmpty()) {
            return new ProjectPostEvaluationVO();
        }
        ProjectPostEvaluationVO response = new ProjectPostEvaluationVO();
        ProjectPostEvaluationVO first = list.get(0);
        response.setProjectName(first.getProjectName());       // 项目名称
        response.setProjectCode(first.getProjectCode());       // 项目编号
        response.setPrimaryEvaluator(first.getPrimaryEvaluator());//评审人数

        Set<String> times = new HashSet<>();

        for (ProjectPostEvaluationVO project : list) {
            addIfNotNull(times, project.getReportSubmissionTime());//评审时间
        }

        response.setReportSubmissionTime(joinSet(times));//评审时间
        return response;
    }



    private void addIfNotNull(Set<String> set, String value) {
        if (value != null && !value.trim().isEmpty()) {
            set.add(value.trim());
        }
    }

    private String joinSet(Set<String> set) {
        if (set == null || set.isEmpty()) {
            return "";
        }
        return String.join("/", set);
    }

    @Override
    public IPage<ProjectPostEvaluationVO> personList(ProjectPostEvaluationVO query) {
        int offset = (int) ((query.getPageNo() - 1) * query.getPageSize());
        int pageSize = Math.toIntExact(query.getPageSize());
        int total = projectPostEvaluationMapper.countUnionList(query);
        List<ProjectPostEvaluationVO> records = projectPostEvaluationMapper.personList(query, offset, pageSize);
        Page<ProjectPostEvaluationVO> page = new Page<>(query.getPageNo(), pageSize);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }
}
